#include <libmedia/video_frame.h>
#include <libmedia/rotate_video.h>
#include <libmedia/media_alloter.h>
#include <libmedia/video_rotater.h>
#include <libmedia/rotate_video.h>

#include "media_soft_rotater.h"
#include "debug/debug.h"

#define DEFAULT_PADDING_COLOR (0x008080)

typedef int (*rotate_func)(
    uint8_t** src, uint32_t* src_linesz, int src_width, int src_height,
    uint8_t** dst, uint32_t* dst_linesz, int dst_width, int dst_height,
    float angle, uint32_t color);

int media_soft_rotater(struct video_frame *src_frame, struct video_frame **dst_frame, int16_t angle)
{
    int ret = -1;
    rotate_func rotate = NULL;

    assert(src_frame);

    switch (src_frame->format) {
    case VIDEO_nv12:  rotate = rotate_nv12; break;
    case VIDEO_y16:   rotate = rotate_y16; break;
    case VIDEO_y8:    rotate = rotate_y8; break;
    case VIDEO_alpha: rotate = rotate_y8; break;
    case VIDEO_bgra:  rotate = rotate_bgra; break;
    default: break;
    }

    if (rotate == NULL)
    {
        print_error("unsupport rotate format %s.", video_fmt_name(src_frame->format));
        return ret;
    }

    struct video_frame* frame = media_alloter_alloc_video_frame(NULL, src_frame->width, src_frame->height, src_frame->format, 0);
    if (!frame) {
        print_error("soft rotater: faield to alloc rotate mem.");
        return ret;
    }
  
    rotate(src_frame->data, src_frame->linesize, src_frame->width, src_frame->height, \
        frame->data, frame->linesize, frame->width, frame->height, angle, DEFAULT_PADDING_COLOR);
  
 
    *dst_frame = frame;

    return 0;

}